package es.udc.emarketapp.http.controller.actions.shippings;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;

import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;
import es.udc.emarketapp.http.controller.session.SessionManager;
import es.udc.emarketapp.http.view.actionForms.ShippingForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class EditShippingAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException, InternalErrorException {


        ShippingForm shippingForm = (ShippingForm) form;

        DecimalFormatSymbols simbolos = new DecimalFormatSymbols();
        simbolos.setDecimalSeparator(',');
        DecimalFormat formateador = new DecimalFormat("####.####", simbolos);
        shippingForm.setPrice1(formateador.format(shippingForm.getPrice1AsDouble()));
        shippingForm.setPrice2(formateador.format(shippingForm.getPrice2AsDouble()));
        shippingForm.setPrice3(formateador.format(shippingForm.getPrice3AsDouble()));
        shippingForm.setPrice4(formateador.format(shippingForm.getPrice4AsDouble()));
        shippingForm.setPrice5(formateador.format(shippingForm.getPrice5AsDouble()));

        try {
            CountryFacade countryFacade = new CountryFacade();
            Locale locale = getLocale(request);
            List<CountryVO> countryVOs =
                    countryFacade.findShippingCountriesAction(
                    SessionManager.getIdUser(request), locale.getLanguage());
            request.setAttribute("countries", countryVOs);

        } catch (Exception e) {
            return mapping.findForward("Failure");
        }

        return mapping.findForward("updateShippingForm");
    }
}
